{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Global seed set to 625\n"
     ]
    }
   ],
   "source": [
    "from pytorch_lightning  import seed_everything\n",
    "import numpy as np\n",
    "import torch\n",
    "\n",
    "import pandas as pd\n",
    "from processing_utils import *\n",
    "from pytorch_widedeep.preprocessing import TabPreprocessor\n",
    "from pytorch_lightning.loggers import TensorBoardLogger\n",
    "from models import *\n",
    "from model_utils import *\n",
    "\n",
    "seed  = 625\n",
    "seed_everything(seed, workers=True)\n",
    "np.random.seed(seed)\n",
    "torch.cuda.manual_seed_all(seed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_pickle('/home/yx/肺部并发症预测/Data/data_multi_text.pkl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>BMI</th>\n",
       "      <th>收缩压</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>呼吸</th>\n",
       "      <th>心率</th>\n",
       "      <th>体温</th>\n",
       "      <th>是否使用活性药物</th>\n",
       "      <th>急诊/择期</th>\n",
       "      <th>一般情况</th>\n",
       "      <th>...</th>\n",
       "      <th>急性肾损伤</th>\n",
       "      <th>肺部并发症</th>\n",
       "      <th>年龄_术中</th>\n",
       "      <th>ASA分级</th>\n",
       "      <th>手术时长（分钟）</th>\n",
       "      <th>出血量</th>\n",
       "      <th>术前诊断</th>\n",
       "      <th>拟行手术</th>\n",
       "      <th>po_words</th>\n",
       "      <th>pd_words</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>25.203981</td>\n",
       "      <td>129.0</td>\n",
       "      <td>81.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>110.0</td>\n",
       "      <td>36.4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>67</td>\n",
       "      <td>3</td>\n",
       "      <td>230.316667</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2型糖尿病</td>\n",
       "      <td>根治性远端胃切除术＋D2＋B2重建</td>\n",
       "      <td>[根治性远端胃切除术, d2, b2重建]</td>\n",
       "      <td>[2型糖尿病]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>26.757812</td>\n",
       "      <td>123.0</td>\n",
       "      <td>79.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>36.5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>66</td>\n",
       "      <td>3</td>\n",
       "      <td>288.216667</td>\n",
       "      <td>NaN</td>\n",
       "      <td>胃窦恶性肿瘤</td>\n",
       "      <td>根治性远端胃切除术</td>\n",
       "      <td>[根治性远端胃切除术]</td>\n",
       "      <td>[胃窦恶性肿瘤]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>20.213384</td>\n",
       "      <td>114.0</td>\n",
       "      <td>66.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>35.7</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>70</td>\n",
       "      <td>3</td>\n",
       "      <td>70.033333</td>\n",
       "      <td>NaN</td>\n",
       "      <td>肝细胞癌</td>\n",
       "      <td>胃肠吻合，肝活检术</td>\n",
       "      <td>[胃肠吻合，, 肝活检术]</td>\n",
       "      <td>[肝细胞癌]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>23.051755</td>\n",
       "      <td>117.0</td>\n",
       "      <td>77.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>78.0</td>\n",
       "      <td>36.5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>82</td>\n",
       "      <td>2</td>\n",
       "      <td>100.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>喉肿物</td>\n",
       "      <td>支撑喉镜下激光喉癌切除术3/4喉切除术及喉功能重建术</td>\n",
       "      <td>[支撑喉镜下, 激光喉癌切除术, 喉切除术, 喉功能重建术]</td>\n",
       "      <td>[喉肿物]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>15.943878</td>\n",
       "      <td>124.0</td>\n",
       "      <td>70.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>80.0</td>\n",
       "      <td>36.7</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>73</td>\n",
       "      <td>2</td>\n",
       "      <td>34.983333</td>\n",
       "      <td>NaN</td>\n",
       "      <td>胃体恶性肿瘤</td>\n",
       "      <td>腹腔镜探查</td>\n",
       "      <td>[腹腔镜探查]</td>\n",
       "      <td>[胃体恶性肿瘤]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17352</th>\n",
       "      <td>2</td>\n",
       "      <td>22.558610</td>\n",
       "      <td>135.0</td>\n",
       "      <td>69.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>91.0</td>\n",
       "      <td>36.3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>69</td>\n",
       "      <td>2</td>\n",
       "      <td>399.333333</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.左肺下叶结节：曲霉菌？ 2.支气管扩张伴感染 3.肺气肿 4.高血压病</td>\n",
       "      <td>胸腔镜左肺下叶切除、胸膜粘连烙断、肺修补</td>\n",
       "      <td>[胸腔镜, 左肺下叶切除, 胸膜粘连烙断, 肺修补]</td>\n",
       "      <td>[左肺下叶结节, 曲霉菌, 支气管扩张, 肺气肿, 高血压病]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17353</th>\n",
       "      <td>1</td>\n",
       "      <td>22.761468</td>\n",
       "      <td>157.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>75.0</td>\n",
       "      <td>36.3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>75</td>\n",
       "      <td>3</td>\n",
       "      <td>204.950000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1:右肾盂癌，高血压，糖尿病</td>\n",
       "      <td>腹腔镜右肾盂癌根治术+右肾周粘连松解术+肠粘连松解术</td>\n",
       "      <td>[腹腔镜右肾盂癌根治术, 右肾周粘连松解术, 肠粘连松解术]</td>\n",
       "      <td>[右肾盂癌, 高血压, 糖尿病]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17354</th>\n",
       "      <td>2</td>\n",
       "      <td>27.005131</td>\n",
       "      <td>125.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>36.5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>82</td>\n",
       "      <td>3</td>\n",
       "      <td>89.333333</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1:左侧胫骨平台骨折 2:高血压病2级 很高危</td>\n",
       "      <td>左侧胫骨平台骨折切开复位内固定术+膝关节清理+同种骨植骨术</td>\n",
       "      <td>[左侧胫骨平台骨折切开复位内固定术, 膝关节清理, 同种骨植骨术]</td>\n",
       "      <td>[左侧胫骨平台骨折, 高血压病]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17355</th>\n",
       "      <td>1</td>\n",
       "      <td>29.136316</td>\n",
       "      <td>132.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>36.5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>69</td>\n",
       "      <td>3</td>\n",
       "      <td>60.750000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1:1.急性胰腺炎、2.胆总管结石伴感染、3. 胃癌术后、4. 高血压、5. 糖尿病。</td>\n",
       "      <td>肠粘连松解;腹腔镜胆囊切除术</td>\n",
       "      <td>[肠粘连松解, 腹腔镜胆囊切除术]</td>\n",
       "      <td>[急性, 胰腺炎, 胆总管结石, 胃癌, 高血压, 糖尿病]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17356</th>\n",
       "      <td>1</td>\n",
       "      <td>23.140496</td>\n",
       "      <td>125.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>80.0</td>\n",
       "      <td>36.2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>66</td>\n",
       "      <td>2</td>\n",
       "      <td>43.950000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1:双肾结石</td>\n",
       "      <td>经尿道输尿管软镜左肾结石钬激光碎石取石术</td>\n",
       "      <td>[经尿道输尿管软镜左肾结石, 激光碎石取石术]</td>\n",
       "      <td>[双肾结石]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>17356 rows × 117 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       性别        BMI    收缩压   舒张压    呼吸     心率    体温  是否使用活性药物  急诊/择期  一般情况  \\\n",
       "0       2  25.203981  129.0  81.0  20.0  110.0  36.4         0      0   2.0   \n",
       "1       1  26.757812  123.0  79.0  20.0   74.0  36.5         0      0   1.0   \n",
       "2       1  20.213384  114.0  66.0  20.0   50.0  35.7         0      0   1.0   \n",
       "3       1  23.051755  117.0  77.0  20.0   78.0  36.5         0      0   2.0   \n",
       "4       1  15.943878  124.0  70.0  20.0   80.0  36.7         0      0   2.0   \n",
       "...    ..        ...    ...   ...   ...    ...   ...       ...    ...   ...   \n",
       "17352   2  22.558610  135.0  69.0  18.0   91.0  36.3         0      0   1.0   \n",
       "17353   1  22.761468  157.0  71.0  20.0   75.0  36.3         0      0   1.0   \n",
       "17354   2  27.005131  125.0  74.0  20.0   84.0  36.5         0      0   2.0   \n",
       "17355   1  29.136316  132.0  71.0  14.0   82.0  36.5         0      0   1.0   \n",
       "17356   1  23.140496  125.0  84.0  18.0   80.0  36.2         0      0   1.0   \n",
       "\n",
       "       ...  急性肾损伤  肺部并发症  年龄_术中  ASA分级    手术时长（分钟）  出血量  \\\n",
       "0      ...      0      0     67      3  230.316667  NaN   \n",
       "1      ...      0      0     66      3  288.216667  NaN   \n",
       "2      ...      0      0     70      3   70.033333  NaN   \n",
       "3      ...      0      0     82      2  100.000000  NaN   \n",
       "4      ...      0      0     73      2   34.983333  NaN   \n",
       "...    ...    ...    ...    ...    ...         ...  ...   \n",
       "17352  ...      0      0     69      2  399.333333  NaN   \n",
       "17353  ...      0      0     75      3  204.950000  NaN   \n",
       "17354  ...      0      0     82      3   89.333333  NaN   \n",
       "17355  ...      0      0     69      3   60.750000  NaN   \n",
       "17356  ...      0      0     66      2   43.950000  NaN   \n",
       "\n",
       "                                              术前诊断  \\\n",
       "0                                            2型糖尿病   \n",
       "1                                           胃窦恶性肿瘤   \n",
       "2                                             肝细胞癌   \n",
       "3                                              喉肿物   \n",
       "4                                           胃体恶性肿瘤   \n",
       "...                                            ...   \n",
       "17352        1.左肺下叶结节：曲霉菌？ 2.支气管扩张伴感染 3.肺气肿 4.高血压病   \n",
       "17353                               1:右肾盂癌，高血压，糖尿病   \n",
       "17354                      1:左侧胫骨平台骨折 2:高血压病2级 很高危   \n",
       "17355  1:1.急性胰腺炎、2.胆总管结石伴感染、3. 胃癌术后、4. 高血压、5. 糖尿病。   \n",
       "17356                                       1:双肾结石   \n",
       "\n",
       "                                拟行手术                           po_words  \\\n",
       "0                  根治性远端胃切除术＋D2＋B2重建              [根治性远端胃切除术, d2, b2重建]   \n",
       "1                          根治性远端胃切除术                        [根治性远端胃切除术]   \n",
       "2                          胃肠吻合，肝活检术                      [胃肠吻合，, 肝活检术]   \n",
       "3         支撑喉镜下激光喉癌切除术3/4喉切除术及喉功能重建术     [支撑喉镜下, 激光喉癌切除术, 喉切除术, 喉功能重建术]   \n",
       "4                              腹腔镜探查                            [腹腔镜探查]   \n",
       "...                              ...                                ...   \n",
       "17352           胸腔镜左肺下叶切除、胸膜粘连烙断、肺修补         [胸腔镜, 左肺下叶切除, 胸膜粘连烙断, 肺修补]   \n",
       "17353     腹腔镜右肾盂癌根治术+右肾周粘连松解术+肠粘连松解术     [腹腔镜右肾盂癌根治术, 右肾周粘连松解术, 肠粘连松解术]   \n",
       "17354  左侧胫骨平台骨折切开复位内固定术+膝关节清理+同种骨植骨术  [左侧胫骨平台骨折切开复位内固定术, 膝关节清理, 同种骨植骨术]   \n",
       "17355                 肠粘连松解;腹腔镜胆囊切除术                  [肠粘连松解, 腹腔镜胆囊切除术]   \n",
       "17356           经尿道输尿管软镜左肾结石钬激光碎石取石术            [经尿道输尿管软镜左肾结石, 激光碎石取石术]   \n",
       "\n",
       "                              pd_words  \n",
       "0                              [2型糖尿病]  \n",
       "1                             [胃窦恶性肿瘤]  \n",
       "2                               [肝细胞癌]  \n",
       "3                                [喉肿物]  \n",
       "4                             [胃体恶性肿瘤]  \n",
       "...                                ...  \n",
       "17352  [左肺下叶结节, 曲霉菌, 支气管扩张, 肺气肿, 高血压病]  \n",
       "17353                 [右肾盂癌, 高血压, 糖尿病]  \n",
       "17354                 [左侧胫骨平台骨折, 高血压病]  \n",
       "17355   [急性, 胰腺炎, 胆总管结石, 胃癌, 高血压, 糖尿病]  \n",
       "17356                           [双肾结石]  \n",
       "\n",
       "[17356 rows x 117 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "entity_vocab_po = get_vocab(df, 'po_words')\n",
    "entity_vocab_pd = get_vocab(df, 'pd_words')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# labels = ['死亡', '肺部并发症', '严重心血管不良', '急性肾损伤']\n",
    "label = '肺部并发症'\n",
    "df_label = get_lable_data(label, df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "words_po = df_label.pop('po_words')\n",
    "words_pd = df_label.pop('pd_words')\n",
    "text_pd =  df_label.pop('术前诊断').fillna(\"无\")\n",
    "text_po =  df_label.pop('拟行手术').fillna(\"无\")\n",
    "y = df_label.pop(label).values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "pre_model, text_pd, text_po = get_text_model(text_pd, text_po)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "words_ids_pd = get_entity_id(words_pd, entity_vocab_pd)\n",
    "words_ids_po = get_entity_id(words_pd, entity_vocab_po)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['pd_words_id'] = words_ids_pd\n",
    "df['po_words_id'] = words_ids_po"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv('/home/yx/肺部并发症预测/Data/data_multi_text_ids.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "cat, cont = cat_cont_split(df_label) \n",
    "df_label_remove = remove_outliers(df_label, cont) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "method = \"cart\"\n",
    "for num in range(len(cont)):\n",
    "    dtype=\"numerical\"\n",
    "    binning(df_label_remove, cont, num, method, y, dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# df = cont_bin(df, cont, 'quantile')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "tab_preprocessor = TabPreprocessor(embed_cols=df_label_remove.columns,  \n",
    "                                    for_transformer=True\n",
    "                                )\n",
    "X_tab = tab_preprocessor.fit_transform(df_label_remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_tab_train, X_tab_test, y_train_valid, y_test, text_pd_train, text_pd_test, words_ids_pd_train, words_ids_pd_test, text_po_train, text_po_test, words_ids_po_train, words_ids_po_test = time_split(X_tab, text_pd, text_po, words_ids_pd, words_ids_po, y, 13904)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "b_size = 2\n",
    "lr = 3e-5\n",
    "epoch = 1000\n",
    "agd = 1\n",
    "dropout = 0.9\n",
    "weight_decay = 0.01\n",
    "use_text = True\n",
    "use_entity = True\n",
    "use_local_attention = True\n",
    "use_global_attention = True\n",
    "vocab_pd_len = entity_vocab_pd.__len__() \n",
    "vocab_po_len = entity_vocab_po.__len__() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using 16bit native Automatic Mixed Precision (AMP)\n",
      "GPU available: True, used: True\n",
      "TPU available: False, using: 0 TPU cores\n",
      "IPU available: False, using: 0 IPUs\n",
      "LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0]\n",
      "\n",
      "   | Name                     | Type               | Params\n",
      "-----------------------------------------------------------------\n",
      "0  | train_metrics            | MetricCollection   | 0     \n",
      "1  | valid_metrics            | MetricCollection   | 0     \n",
      "2  | pre_model                | BertForPreTraining | 102 M \n",
      "3  | cat_embed                | Embedding          | 5.4 K \n",
      "4  | embedding_dropout        | Dropout            | 0     \n",
      "5  | transformer_text         | TransformerEncoder | 600   \n",
      "6  | transformer_tabular      | TransformerEncoder | 600   \n",
      "7  | transformer_text_tabular | TransformerEncoder | 600   \n",
      "8  | logits                   | LogSoftmax         | 0     \n",
      "9  | liner                    | Linear             | 3.9 K \n",
      "10 | sentence_liner           | Linear             | 6.2 K \n",
      "11 | transformer_all          | TransformerEncoder | 600   \n",
      "12 | transformer_mlp          | MLP                | 3.8 M \n",
      "13 | entity_pd_embed          | Embedding          | 106 K \n",
      "14 | entity_po_embed          | Embedding          | 65.4 K\n",
      "-----------------------------------------------------------------\n",
      "4.0 M     Trainable params\n",
      "102 M     Non-trainable params\n",
      "106 M     Total params\n",
      "213.831   Total estimated model params size (MB)\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6f99d81a4e3546eb9bb65879dbc29589",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Validation sanity check: 0it [00:00, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yx/.conda/envs/ppc_env/lib/python3.8/site-packages/pytorch_lightning/trainer/data_loading.py:116: UserWarning: The dataloader, val_dataloader 0, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.\n",
      "  rank_zero_warn(\n",
      "Global seed set to 625\n",
      "/home/yx/.conda/envs/ppc_env/lib/python3.8/site-packages/pytorch_lightning/trainer/data_loading.py:116: UserWarning: The dataloader, train_dataloader, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 12 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.\n",
      "  rank_zero_warn(\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "dae75406d5c94b05b6b73201f6057c70",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Training: 0it [00:00, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f9e8a155d577407b90964d23662cc867",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Validating: 0it [00:00, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yx/.conda/envs/ppc_env/lib/python3.8/site-packages/pytorch_lightning/trainer/trainer.py:685: UserWarning: Detected KeyboardInterrupt, attempting graceful shutdown...\n",
      "  rank_zero_warn(\"Detected KeyboardInterrupt, attempting graceful shutdown...\")\n"
     ]
    }
   ],
   "source": [
    "kf = KFold(n_splits=5, shuffle = True, random_state = 625)\n",
    "results = []\n",
    "n = 0\n",
    "data_loaders = []\n",
    "for train_index, valid_index in kf.split(X_tab_train):\n",
    "    \n",
    "    n += 1\n",
    "\n",
    "    data_loader_train, data_loader_valid, data_loader_valid_test, data_loader_test = get_Dataloader(\n",
    "        X_tab_train, text_pd_train, text_po_train, np.array(words_ids_pd_train), np.array(words_ids_po_train), y_train_valid, train_index, valid_index, b_size, \n",
    "        X_tab_test, text_pd_test, text_po_test, words_ids_pd_test, words_ids_po_test, y_test\n",
    "    )\n",
    "    \n",
    "    data_loaders.append(data_loader_valid_test)\n",
    "    pt_path = \"model_checkpoint/test\"\n",
    "    pt_name = str(n)+\"_lr=\"+str(lr)+\"_b_size=\"+str(b_size)+\"_agd=\"+str(agd)+\"_dropout=\"+str(dropout)\n",
    "    logger = TensorBoardLogger(pt_path, name = pt_name)\n",
    "    model = NET(\n",
    "        use_text = use_text, use_entity = use_entity, use_local_attention = use_local_attention,use_global_attention=use_global_attention,\n",
    "        vocab_pd_len = vocab_pd_len, vocab_po_len = vocab_po_len, pre_model = pre_model, dropout = dropout, weight_decay = weight_decay, lr = lr,\n",
    "        column_idx=tab_preprocessor.column_idx ,embed_input=tab_preprocessor.embeddings_input)\n",
    "    trainer = get_trainer(agd, logger, epoch)\n",
    "    trainer.fit(model, data_loader_train, data_loader_valid)\n",
    "    torch.cuda.empty_cache()\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "def get_pt():\n",
    "    ckpts = []\n",
    "    for i in range(1, 6):\n",
    "        for filepath,dirnames,filenames in os.walk('/home/yx/肺部并发症预测/model_checkpoint/Tabular/bin_quantile/'+str(i)+'_lr=0.0003_b_size=1024_agd=1_dropout=0.4/version_0/checkpoints'):\n",
    "            for filename in filenames:\n",
    "                ckpts.append(os.path.join(filepath,filename))\n",
    "    return ckpts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_predict(data_loader_test, model):\n",
    "    model.cuda()\n",
    "    model.eval()\n",
    "    P = []\n",
    "    Y = []\n",
    "    with torch.no_grad():\n",
    "        for data in data_loader_test:\n",
    "            X = data[0].cuda()\n",
    "            y = data[1].tolist()[0]\n",
    "            Y.append(y)\n",
    "            log_p = model(X)\n",
    "            p = torch.exp(log_p[0][1]).tolist()\n",
    "            P.append(p)\n",
    "    return P, Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ckpts = get_pt()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_model_result(ckpts, data_loaders):\n",
    "    results = []\n",
    "    for pt, data_loader in zip(ckpts, data_loaders):\n",
    "        model = Tabular.load_from_checkpoint(pt, dropout = dropout, lr = lr, column_idx=tab_preprocessor.column_idx, embed_input=tab_preprocessor.embeddings_input)\n",
    "        P, Y = get_predict(data_loader, model)\n",
    "        result = get_metrics(Y, P)\n",
    "        results.append(result)\n",
    "    df_result = get_result(results)\n",
    "    return df_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ckpts = get_pt('/home/yx/3090/project/P_prediction/肺部并发症预测/model_checkpoint/')\n",
    "df_result = get_model_result(ckpts, data_loaders)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_result['model'] = 'deep-learning_bin'\n",
    "df_result['text'] = 'no'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_result.to_excel('/home/yx/肺部并发症预测/Data/dnn_quantile.xlsx')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.13 ('P_prediction')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "676c8c2553a8289d770cd55f8a54cd76a7646969c2ee13a0fd2fdbb12048cb4a"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
